package com.qianji.petmall.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qianji.common.utils.PageUtils;
import com.qianji.common.utils.Query;
import com.qianji.petmall.order.dao.OrderReturnReasonDao;
import com.qianji.petmall.order.entity.OrderEntity;
import com.qianji.petmall.order.entity.OrderReturnReasonEntity;
import com.qianji.petmall.order.service.OrderReturnReasonService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;


@Service("orderReturnReasonService")
@Slf4j
@RabbitListener(queues = {"hello.java.queue"})
public class OrderReturnReasonServiceImpl extends ServiceImpl<OrderReturnReasonDao, OrderReturnReasonEntity> implements OrderReturnReasonService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderReturnReasonEntity> page = this.page(
                new Query<OrderReturnReasonEntity>().getPage(params),
                new QueryWrapper<OrderReturnReasonEntity>()
        );

        return new PageUtils(page);
    }

    //Channel 当前传输数据的通道
    /**
     * 1、Mcssage message:原生消息详细信息。头+体
     * 2、T<发送的消息的类型>orderReturnReasonEntity content;3、Channel channel:当前传输数据的通道
     * 3、Queue:可以很多人都来监听。只要收到消息，队列删除消息，而且只能有一个收到此消息场景:
     * 1)、订单服务启动多个;同一个消息,只能有一个客户端收到
     * 2)、只有一个消息完全处理完，方法运行结束，我们就可以接收到下一个消息
     */
    @RabbitHandler
    public void test(Message message, OrderReturnReasonEntity orderReturnReason, Channel channel) {
        //消息体信息
        System.out.println("内容"+orderReturnReason);
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
        //自增的按顺序自增
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        System.out.println("deliveryTag{"+deliveryTag+"}");
        //签收货物
        try {
            if (deliveryTag%2==0){
                //                public void basicNack(long deliveryTag, //channel内按顺序自增
//                boolean multiple,                       //是否批量退货
//                boolean requeue)                        //确认后是否重新入队 false丢弃
                channel.basicAck(deliveryTag,false);
                System.out.println("签收货物成功"+deliveryTag);
            }else {
                //退货
                channel.basicNack(deliveryTag,false,false);
                System.out.println("没有签收货物"+deliveryTag);
            }

        } catch (IOException e) {
            //网络中断
            e.printStackTrace();
        }
//        Thread.sleep(3000);
//        System.out.println("接受的消息完成"+orderReturnReason.getName());
    }

    @RabbitHandler
    public void test2(Message message, OrderEntity order, Channel channel) throws InterruptedException {
        //消息体信息
        System.out.println("内容"+order);
        byte[] body = message.getBody();
        //消息头属性信息
        MessageProperties messageProperties = message.getMessageProperties();
//        Thread.sleep(3000);
//        System.out.println("接受的消息完成"+order.getOrderSn());
    }

}